package cn.lcc.org.controller;

import cn.lcc.basic.util.AjaxResult;
import cn.lcc.basic.util.PageList;
import cn.lcc.org.domain.Employee;
import cn.lcc.org.query.EmployeeQuery;
import cn.lcc.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "部门接口",description = "处理部门的所有请求")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    //查询单个对象
    //写一个接口/方法：1.请求方式 2.请求参数 3.请求地址 4.返回数据格式
    //Get http://localhost/employee/1
    @GetMapping("/{id}")
    @ApiOperation("查询单个对象")
    public Employee loadById(@PathVariable("id") Long id){
        return employeeService.loadById(id);
    }
    //查询所有对象
    //Get http://localhost/employee
    @GetMapping
    @ApiOperation("查询所有对象")
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }
    //删除对象
    //delete http://localhost/employee/1
    @DeleteMapping("/{id}")
    @ApiOperation("删除单个对象")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }

    }
    //修改和新增
    //put http://localhost/employee {对象}
    @PutMapping
    @ApiOperation("添加或者修改")
    public AjaxResult saveAndUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }

    }
    //分页查询
    //post http://localhost/employee 对象
    @PostMapping
    @ApiOperation("查询分页")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }
    @GetMapping("/list/{shopId}")
    public List<Employee> list(@PathVariable("shopId") Long shopId){
        return employeeService.list(shopId);
    }
}
